/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.cux.workflow.service.spring;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import org.redragon.framework.annotation.Cache;
import org.redragon.framework.annotation.Cache.CacheType;
import org.redragon.framework.annotation.Log;
import org.redragon.framework.hibernate.model.Pages;
import com.erp.cux.workflow.dao.CuxWorkflowRecordDao;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfHead;
import com.erp.cux.workflow.dao.model.CuxWorkflowConfLine;
import com.erp.cux.workflow.dao.model.CuxWorkflowRecord;
import com.erp.cux.workflow.dao.model.CuxWorkflowRecordCO;
import com.erp.cux.workflow.service.CuxWorkflowConfHeadService;
import com.erp.cux.workflow.service.CuxWorkflowConfLineService;
import com.erp.cux.workflow.service.CuxWorkflowRecordService;

@Service
@Transactional(rollbackFor=Exception.class)
public class CuxWorkflowRecordServiceImpl implements CuxWorkflowRecordService {

    //注入Dao
    @Autowired
    private CuxWorkflowRecordDao cuxWorkflowRecordDao;
    @Autowired
    private CuxWorkflowConfHeadService cuxWorkflowConfHeadService;
    @Autowired
    private CuxWorkflowConfLineService cuxWorkflowConfLineService;
    
    @Override
    public void insertDataObject(CuxWorkflowRecord obj) {
        this.cuxWorkflowRecordDao.insertDataObject(obj);
    }

    @Override
    public void updateDataObject(CuxWorkflowRecord obj) {
        this.cuxWorkflowRecordDao.updateDataObject(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(CuxWorkflowRecord obj) {
        this.cuxWorkflowRecordDao.insertOrUpdateDataObject(obj);
    }

    @Override
    public void deleteDataObject(CuxWorkflowRecord obj) {
        this.cuxWorkflowRecordDao.deleteDataObject(obj);
    }

    @Override
    public List<CuxWorkflowRecord> getDataObjects() {
        return this.cuxWorkflowRecordDao.getDataObjects();
    }

    @Override
    public CuxWorkflowRecord getDataObject(int id) {
        return this.cuxWorkflowRecordDao.getDataObject(id);
    }

    @Override
    public CuxWorkflowRecord getDataObject(String code) {
        return this.cuxWorkflowRecordDao.getDataObject(code);
    }

    @Override
    public List<CuxWorkflowRecord> getDataObjects(CuxWorkflowRecordCO paramObj) {
        return this.cuxWorkflowRecordDao.getDataObjects(paramObj);
    }

    @Override
    public List<CuxWorkflowRecord> getDataObjects(Pages pages) {
        return this.cuxWorkflowRecordDao.getDataObjects(pages);
    }
    
    @Override
    public List<CuxWorkflowRecord> getDataObjects(Pages pages, CuxWorkflowRecordCO paramObj) {
        return this.cuxWorkflowRecordDao.getDataObjects(pages, paramObj);
    }
    
    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, CuxWorkflowRecordCO paramObj) {
        return this.cuxWorkflowRecordDao.getDataObjectsArray(pages, paramObj);
    }
    
    @Override
    public List<CuxWorkflowRecord> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, CuxWorkflowRecordCO paramObj) {
        return this.cuxWorkflowRecordDao.getDataObjectsForDataAuth(dataAuthSQL, pages, paramObj);
    }
    
    @Override
    public List<String> getBillHeadCodesByPerson(String billType, String approvePerson) {
        return this.cuxWorkflowRecordDao.getBillHeadCodesByPerson(billType, approvePerson);
    }
    
    @Override
    public CuxWorkflowRecord getCurrentRecordByBill(String billType, String billHeadCode) {
        return this.cuxWorkflowRecordDao.getCurrentRecordByBill(billType, billHeadCode);
    }
    
    @Override
    public CuxWorkflowRecord getLastRecordByBill(String billType, String billHeadCode) {
        return this.cuxWorkflowRecordDao.getLastRecordByBill(billType, billHeadCode);
    }
    
    @Override
    public void approveWorkflowRecode(CuxWorkflowRecord cuxWorkflowRecord) {
        
        if(cuxWorkflowRecord!=null&&StringUtils.isNotBlank(cuxWorkflowRecord.getBillHeadCode())&&StringUtils.isNotBlank(cuxWorkflowRecord.getBillType())) {
            //获取当前节点
            CuxWorkflowRecord currentRecord = this.cuxWorkflowRecordDao.getCurrentRecordByBill(cuxWorkflowRecord.getBillType(), cuxWorkflowRecord.getBillHeadCode());
            //更新节点的审批结果
            currentRecord.setApproveResult(cuxWorkflowRecord.getApproveResult());
            currentRecord.setApproveComment(cuxWorkflowRecord.getApproveComment());
            this.cuxWorkflowRecordDao.updateDataObject(currentRecord);
            
            //新增下一节点记录
            cuxWorkflowRecord.setApproveResult(null);
            cuxWorkflowRecord.setApproveComment(null);
            this.cuxWorkflowRecordDao.insertDataObject(cuxWorkflowRecord);
        }
        
    }
    
    @Override
    public void deleteRecordByBill(String billType, String billHeadCode) {
        this.cuxWorkflowRecordDao.deleteRecordByBill(billType, billHeadCode);
    }
    
}